public class myProc
{
    public delegate void DelegateMethod(string message);
    private static System.Diagnostics.Process objProc;
    private static DelegateMethod objHnd;
    private static bool blRunning;


    /// <summary>
    /// 执行一个带有参数和工作目录的外部程序。为了避免运行异常，调用前请先检验程序文件是否存在。
    /// </summary>
    /// <param name="argUrl">程序文件名，若指定了工作文件夹，则可以使用相对路径。</param>
    /// <param name="argArg">命令行参数。</param>
    /// <param name="argPwd">工作目录。</param>
    public void run(string argUrl, string argArg, string argPwd)
    {
        blRunning = false;
        objProc = new System.Diagnostics.Process();
        objProc.StartInfo.CreateNoWindow = true;
        objProc.StartInfo.UseShellExecute = false;
        objProc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        objProc.StartInfo.FileName = argUrl;
        objProc.StartInfo.Arguments = argArg;
        objProc.StartInfo.WorkingDirectory = argPwd;
        objProc.EnableRaisingEvents = true;
        objProc.StartInfo.RedirectStandardError = true;
        objProc.StartInfo.RedirectStandardOutput = true;
        objProc.StartInfo.RedirectStandardInput = false;
        objProc.ErrorDataReceived   += new System.Diagnostics.DataReceivedEventHandler(_hndErrOut);
        objProc.OutputDataReceived  += new System.Diagnostics.DataReceivedEventHandler(_hndStdOut);
        objProc.Exited              += new EventHandler(_hndExit);
        objProc.Disposed            += new EventHandler(_hndDisp);
        try
        {
            objProc.Start();
        }
        catch (System.ComponentModel.Win32Exception)
        {
            // if (objHnd != null) objHnd("myProc.exception.");
            throw;
        }
        objProc.BeginErrorReadLine();
        objProc.BeginOutputReadLine();
        blRunning = true;
    }


    /// <summary>
    /// 杀死正在运行的当前进程。如果进程不在运行，则不做任何动作。
    /// </summary>
    public void kill()
    {
        if (blRunning == true)
            if (objProc.HasExited == false)
            {
                objProc.Kill();
            }
    }


    /// <summary>
    /// 挂载事件处理程序。
    /// </summary>
    /// <param name="hnd">Delegate Method类型的事件处理程序，带有一个字符型参数用来传递消息。</param>
    public void hnd(DelegateMethod hnd)
    {
        objHnd = hnd;
    }


    private static void _hndErrOut(object sender, System.Diagnostics.DataReceivedEventArgs e)
    {
        if (objHnd != null && e.Data != null)
            objHnd("myProc.errOut:" + e.Data.ToString());
    }


    private static void _hndStdOut(object sender, System.Diagnostics.DataReceivedEventArgs e)
    {
        if (objHnd != null && e.Data != null)
            objHnd("myProc.stdOut:" + e.Data.ToString());
    }


    private static void _hndExit(object sender, EventArgs e)
    {
        if (objHnd != null)
        {
            objProc.ErrorDataReceived    -= new System.Diagnostics.DataReceivedEventHandler(_hndErrOut);
            objProc.OutputDataReceived   -= new System.Diagnostics.DataReceivedEventHandler(_hndStdOut);
            objProc.Exited               -= new EventHandler(_hndExit);
            objProc.Disposed             -= new EventHandler(_hndDisp);
            objProc.Close();
            blRunning = false;
            objHnd("myProc.exited");
        }
    }


    private static void _hndDisp(object sender, EventArgs e)
    {
        if (objHnd != null)
            objHnd("myProc.disposed");
    }
}
